﻿using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using AnyOffice.BIZ;
using log4net;
using System.Reflection;


namespace AnyOffice.general.hrms.manage
{
    public partial class edit : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected string strTemp;

        protected void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.AutoCreateDirectory = true;
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    ConfigManager manager = new ConfigManager();
                    upload.MaxSize = manager.AttachMaxSize;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ConfigManager manager = new ConfigManager();
            if (this.filePhoto.PostedFile.ContentLength > 0)
            {
                if (this.filePhoto.PostedFile.ContentLength > manager.ImageUploadMaxSize)
                {
                    this.litMessage.Text = base.ShowPrompt("照片大小超过500k！");
                    return;
                }
                if (!this.O10O00O(this.filePhoto.PostedFile.ContentType))
                {
                    this.litMessage.Text = base.ShowPrompt("照片格式不正确！");
                    return;
                }
            }
            string attachmentId = "";
            try
            {
                if (this.ViewState["OldAttachmentId"].ToString() != string.Empty)
                {
                    AttachmentBiz.DeleteByAttachmendId(base.Conn, this.ViewState["OldAttachmentId"].ToString());
                }
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 10;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("表单提交 - 附件上传相关", exception);
                }
            }
            HrmsDao dao2 = new HrmsDao();
            HrmsData data2 = new HrmsData();
            data2.HrmsId = Convert.ToInt32(this.ViewState["HrmsId"].ToString());
            if (this.UserId.Value != string.Empty)
            {
                data2.UserId = this.UserId.Value;
            }
            data2.UserName = this.txtUserName.Text;
            if (this.txtNumber.Value != string.Empty)
            {
                data2.Number = this.txtNumber.Value;
            }
            data2.DeptId = Convert.ToInt32(this.ddlDeptId.SelectedValue);
            data2.Sex = Convert.ToBoolean(this.selectSex.Value);
            if (this.txtBirthday.Value != string.Empty)
            {
                data2.Birthday = Convert.ToDateTime(this.txtBirthday.Value);
            }
            if (this.filePhoto.PostedFile.ContentLength > 0)
            {
                int contentLength = this.filePhoto.PostedFile.ContentLength;
                byte[] buffer = new byte[contentLength];
                this.filePhoto.PostedFile.InputStream.Read(buffer, 0, contentLength);
                data2.Photo = buffer;
            }
            if (this.txtNation.Value != string.Empty)
            {
                data2.Nation = this.txtNation.Value;
            }
            if (this.txtCardNo.Value != string.Empty)
            {
                data2.CardNo = this.txtCardNo.Value;
            }
            if (this.selectMarry.Value != string.Empty)
            {
                data2.Marry = this.selectMarry.Value;
            }
            if (this.selectPolitics.Value != string.Empty)
            {
                data2.Politics = this.selectPolitics.Value;
            }
            if (this.txtNativePlace.Value != string.Empty)
            {
                data2.NativePlace = this.txtNativePlace.Value;
            }
            if (this.txtHukouPlace.Value != string.Empty)
            {
                data2.HukouPlace = this.txtHukouPlace.Value;
            }
            if (this.selectEducation.Value != string.Empty)
            {
                data2.Education = this.selectEducation.Value;
            }
            if (this.txtTechPost.Value != string.Empty)
            {
                data2.TechPost = this.txtTechPost.Value;
            }
            if (this.txtSchool.Value != string.Empty)
            {
                data2.School = this.txtSchool.Value;
            }
            if (this.txtSpeciallity.Value != string.Empty)
            {
                data2.Speciallity = this.txtSpeciallity.Value;
            }
            if (this.txtWorkDate.Value != string.Empty)
            {
                data2.WorkDate = Convert.ToDateTime(this.txtWorkDate.Value);
            }
            if (this.txtJoinDate.Value != string.Empty)
            {
                data2.JoinDate = Convert.ToDateTime(this.txtJoinDate.Value);
            }
            if (this.txtContractType.Value != string.Empty)
            {
                data2.ContractType = this.txtContractType.Value;
            }
            if (this.txtPost.Value != string.Empty)
            {
                data2.Post = this.txtPost.Value;
            }
            if (this.txtHomeTel.Value != string.Empty)
            {
                data2.HomeTel = this.txtHomeTel.Value;
            }
            if (this.txtEmail.Value != string.Empty)
            {
                data2.Email = this.txtEmail.Value;
            }
            if (this.txtHomeAddr.Value != string.Empty)
            {
                data2.HomeAddr = this.txtHomeAddr.Value;
            }
            if (this.txtContractDate1.Value != string.Empty)
            {
                data2.ContractDate1 = Convert.ToDateTime(this.txtContractDate1.Value);
            }
            if (this.txtContractDate2.Value != string.Empty)
            {
                data2.ContractDate2 = Convert.ToDateTime(this.txtContractDate2.Value);
            }
            if (this.txtLeaveType.Value != string.Empty)
            {
                data2.LeaveType = this.txtLeaveType.Value;
            }
            if (this.txtPostChange.Value != string.Empty)
            {
                data2.PostChange = this.txtPostChange.Value;
            }
            if (this.txtEdu.Value != string.Empty)
            {
                data2.Edu = this.txtEdu.Value;
            }
            if (this.txtResume.Value != string.Empty)
            {
                data2.Resume = this.txtResume.Value;
            }
            if (this.txtSociaty.Value != string.Empty)
            {
                data2.Sociaty = this.txtSociaty.Value;
            }
            if (this.txtReward.Value != string.Empty)
            {
                data2.Reward = this.txtReward.Value;
            }
            if (this.txtCertificate.Value != string.Empty)
            {
                data2.Certificate = this.txtCertificate.Value;
            }
            if (this.txtTrain.Value != string.Empty)
            {
                data2.Train = this.txtTrain.Value;
            }
            if (this.txtSurety.Value != string.Empty)
            {
                data2.Surety = this.txtSurety.Value;
            }
            if (this.txtContract.Value != string.Empty)
            {
                data2.Contract = this.txtContract.Value;
            }
            if (this.txtInsure.Value != string.Empty)
            {
                data2.Insure = this.txtInsure.Value;
            }
            if (this.txtBodyExamim.Value != string.Empty)
            {
                data2.BodyExamim = this.txtBodyExamim.Value;
            }
            if (this.txtMemo.Value != string.Empty)
            {
                data2.Memo = this.txtMemo.Value;
            }
            if (attachmentId != string.Empty)
            {
                data2.AttachmentId = attachmentId;
            }
            dao2.Update(base.Conn, data2);
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("学生 " + this.txtUserName.Text + " 的档案已保存！", "dept.aspx?dept_id=" + this.ViewState["DeptId"].ToString());
        }
        private bool O10O00O(string contentType)
        {
            string str;
            return (((str = contentType) != null) && (((str == "image/gif") || (str == "image/jpeg")) || (str == "image/pjpeg")));
        }
        private void OO01l10OO0O01l0(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.OO01l10OO0O01l0(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int hrmsId = (Request.QueryString["hrms_id"] != null) ? int.Parse(Request.QueryString["hrms_id"].ToString()) : 0;
                this.ViewState["HrmsId"] = hrmsId;
                int deptId = (Request.QueryString["dept_id"] != null) ? int.Parse(Request.QueryString["dept_id"].ToString()) : 0;
                this.ViewState["DeptId"] = deptId;
                DataTable table = new HrmsDao().SelectByHrmsId(base.Conn, hrmsId);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无相关学生档案", "dept.aspx?dept_id=" + deptId.ToString());
                }
                else
                {
                    DataTable table2;
                    ListItem item;
                    this.txtUserName.Text = table.Rows[0]["UserName"].ToString();
                    if (table.Rows[0]["UserId"] != DBNull.Value)
                    {
                        table2 = new DepartmentDao().SelectByDeptId(base.Conn, deptId);
                        if (table2.Rows.Count > 0)
                        {
                            item = new ListItem("|─" + table2.Rows[0]["DeptName"].ToString(), table2.Rows[0]["DeptId"].ToString());
                            this.ddlDeptId.Items.Add(item);
                        }
                        this.litUserName.Text = "（" + table.Rows[0]["UserName"].ToString() + "） - 已建档";
                        this.UserId.Value = table.Rows[0]["UserId"].ToString();
                        this.txtUserName.ReadOnly = true;
                        this.txtUserName.CssClass = "BigStatic";
                    }
                    else
                    {
                        item = new ListItem("请选择", "");
                        this.ddlDeptId.Items.Add(item);
                        table2 = new DepartmentDao().SelectParentAll(base.Conn);
                        if (table2.Rows.Count > 0)
                        {
                            for (int i = 0; i < table2.Rows.Count; i++)
                            {
                                item = new ListItem("|─" + table2.Rows[i]["DeptName"].ToString(), table2.Rows[i]["DeptId"].ToString());
                                this.ddlDeptId.Items.Add(item);
                                this.strTemp = "";
                                this.OO01l10OO0O01l0(ref this.ddlDeptId, int.Parse(table2.Rows[i]["DeptId"].ToString()), this.strTemp);
                            }
                        }
                        this.ddlDeptId.Items.Add(new ListItem("离校人员/外部人员", "-1"));
                        if (this.ddlDeptId.Items.FindByValue(deptId.ToString()) != null)
                        {
                            this.ddlDeptId.Items.FindByValue(deptId.ToString()).Selected = true;
                        }
                    }
                    if (table.Rows[0]["Number"] != DBNull.Value)
                    {
                        this.txtNumber.Value = table.Rows[0]["Number"].ToString();
                    }
                    if ((table.Rows[0]["Sex"] != DBNull.Value) && (this.selectSex.Items.FindByValue(table.Rows[0]["Sex"].ToString()) != null))
                    {
                        this.selectSex.Items.FindByValue(table.Rows[0]["Sex"].ToString()).Selected = true;
                    }
                    this.txtBirthday.Value = (table.Rows[0]["Birthday"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["Birthday"].ToString()).ToString("d") : "";
                    if (table.Rows[0]["Photo"] != DBNull.Value)
                    {
                        this.litUpload.Text = "照片更改：";
                        this.litPhoto.Text = "<a href=\"../photo.aspx?hrms_id=" + hrmsId.ToString() + "\" target=\"_blank\" title=\"点击查看放大图片\"><img src=\"../photo.aspx?hrms_id=" + hrmsId.ToString() + "\" width=\"100\" border=\"1\"></a>";
                    }
                    this.txtNation.Value = (table.Rows[0]["Nation"] != DBNull.Value) ? table.Rows[0]["Nation"].ToString() : "";
                    this.txtCardNo.Value = (table.Rows[0]["CardNo"] != DBNull.Value) ? table.Rows[0]["CardNo"].ToString() : "";
                    if ((table.Rows[0]["Marry"] != DBNull.Value) && (this.selectMarry.Items.FindByValue(table.Rows[0]["Marry"].ToString()) != null))
                    {
                        this.selectMarry.Items.FindByValue(table.Rows[0]["Marry"].ToString()).Selected = true;
                    }
                    if ((table.Rows[0]["Politics"] != DBNull.Value) && (this.selectPolitics.Items.FindByValue(table.Rows[0]["Politics"].ToString()) != null))
                    {
                        this.selectPolitics.Items.FindByValue(table.Rows[0]["Politics"].ToString()).Selected = true;
                    }
                    this.txtNativePlace.Value = (table.Rows[0]["NativePlace"] != DBNull.Value) ? table.Rows[0]["NativePlace"].ToString() : "";
                    this.txtHukouPlace.Value = (table.Rows[0]["HukouPlace"] != DBNull.Value) ? table.Rows[0]["HukouPlace"].ToString() : "";
                    if ((table.Rows[0]["Education"] != DBNull.Value) && (this.selectEducation.Items.FindByValue(table.Rows[0]["Education"].ToString()) != null))
                    {
                        this.selectEducation.Items.FindByValue(table.Rows[0]["Education"].ToString()).Selected = true;
                    }
                    this.txtTechPost.Value = (table.Rows[0]["TechPost"] != DBNull.Value) ? table.Rows[0]["TechPost"].ToString() : "";
                    this.txtSchool.Value = (table.Rows[0]["School"] != DBNull.Value) ? table.Rows[0]["School"].ToString() : "";
                    this.txtSpeciallity.Value = (table.Rows[0]["Speciallity"] != DBNull.Value) ? table.Rows[0]["Speciallity"].ToString() : "";
                    this.txtWorkDate.Value = (table.Rows[0]["WorkDate"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["WorkDate"].ToString()).ToString("d") : "";
                    this.txtJoinDate.Value = (table.Rows[0]["JoinDate"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["JoinDate"].ToString()).ToString("d") : "";
                    this.txtContractType.Value = (table.Rows[0]["ContractType"] != DBNull.Value) ? table.Rows[0]["ContractType"].ToString() : "";
                    this.txtPost.Value = (table.Rows[0]["Post"] != DBNull.Value) ? table.Rows[0]["Post"].ToString() : "";
                    this.txtHomeTel.Value = (table.Rows[0]["HomeTel"] != DBNull.Value) ? table.Rows[0]["HomeTel"].ToString() : "";
                    this.txtEmail.Value = (table.Rows[0]["Email"] != DBNull.Value) ? table.Rows[0]["Email"].ToString() : "";
                    this.txtHomeAddr.Value = (table.Rows[0]["HomeAddr"] != DBNull.Value) ? table.Rows[0]["HomeAddr"].ToString() : "";
                    this.txtContractDate1.Value = (table.Rows[0]["ContractDate1"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["ContractDate1"].ToString()).ToString("d") : "";
                    this.txtContractDate2.Value = (table.Rows[0]["ContractDate2"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["ContractDate2"].ToString()).ToString("d") : "";
                    this.txtLeaveType.Value = (table.Rows[0]["LeaveType"] != DBNull.Value) ? table.Rows[0]["LeaveType"].ToString() : "";
                    this.txtPostChange.Value = (table.Rows[0]["PostChange"] != DBNull.Value) ? table.Rows[0]["PostChange"].ToString() : "";
                    this.txtEdu.Value = (table.Rows[0]["Edu"] != DBNull.Value) ? table.Rows[0]["Edu"].ToString() : "";
                    this.txtResume.Value = (table.Rows[0]["Resume"] != DBNull.Value) ? table.Rows[0]["Resume"].ToString() : "";
                    this.txtSociaty.Value = (table.Rows[0]["Sociaty"] != DBNull.Value) ? table.Rows[0]["Sociaty"].ToString() : "";
                    this.txtReward.Value = (table.Rows[0]["Reward"] != DBNull.Value) ? table.Rows[0]["Reward"].ToString() : "";
                    this.txtCertificate.Value = (table.Rows[0]["Certificate"] != DBNull.Value) ? table.Rows[0]["Certificate"].ToString() : "";
                    this.txtTrain.Value = (table.Rows[0]["Train"] != DBNull.Value) ? table.Rows[0]["Train"].ToString() : "";
                    this.txtSurety.Value = (table.Rows[0]["Surety"] != DBNull.Value) ? table.Rows[0]["Surety"].ToString() : "";
                    this.txtContract.Value = (table.Rows[0]["Contract"] != DBNull.Value) ? table.Rows[0]["Contract"].ToString() : "";
                    this.txtInsure.Value = (table.Rows[0]["Insure"] != DBNull.Value) ? table.Rows[0]["Insure"].ToString() : "";
                    this.txtBodyExamim.Value = (table.Rows[0]["BodyExamim"] != DBNull.Value) ? table.Rows[0]["BodyExamim"].ToString() : "";
                    this.txtMemo.Value = (table.Rows[0]["Memo"] != DBNull.Value) ? table.Rows[0]["Memo"].ToString() : "";
                    ConfigManager manager = new ConfigManager();
                    this.litImageMaxSize.Text = (manager.ImageUploadMaxSize / 1024).ToString();
                    string attachmentId = (table.Rows[0]["AttachmentId"] != DBNull.Value) ? table.Rows[0]["AttachmentId"].ToString() : "";
                    this.ViewState["FileNums"] = 0;
                    this.ViewState["FileList"] = new ArrayList();
                    this.ViewState["UniqueTempPath"] = "";
                    this.ViewState["TotalSize"] = 0;
                    this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                    this.ViewState["OldAttachmentId"] = attachmentId;
                    if (attachmentId != string.Empty)
                    {
                        DataTable table3 = new AttachmentDao().SelectByAttachmentId(base.Conn, attachmentId);
                        if (table3.Rows.Count > 0)
                        {
                            try
                            {
                                this.ViewState["FileNums"] = table3.Rows.Count;
                                ArrayList list = new ArrayList();
                                string savedPath = AttachmentBiz.GetSavedPath(attachmentId);
                                string uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                                this.ViewState["UniqueTempPath"] = uniqueTempPath;
                                AttachmentBiz.Copy(savedPath, uniqueTempPath);
                                int num5 = 0;
                                for (int j = 0; j < table3.Rows.Count; j++)
                                {
                                    list.Add(table3.Rows[j]["Name"].ToString());
                                    num5 += Convert.ToInt32(table3.Rows[j]["TotalSize"].ToString());
                                }
                                this.ltbFileList.DataSource = list;
                                this.ltbFileList.DataBind();
                                this.ltbFileList.Rows = table3.Rows.Count;
                                this.ViewState["FileList"] = list;
                                this.ViewState["TotalSize"] = num5;
                                this.pnlAttach.Visible = true;
                            }
                            catch (Exception exception)
                            {
                                if (log.IsErrorEnabled)
                                {
                                    log.Error("提交前 - 附件上传相关", exception);
                                }
                            }
                        }
                    }
                    this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                }
            }
        }
    }
}
